﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SocialIn.Biz.Utils;
using SocialIn.Biz;

namespace SocialIn.Web.Members
{
    public partial class JoinVerify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string key = Request.QueryString["key"];
            string id = Request.QueryString["id"];

            if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(id))
            {
                ErrorMessage("잘못된 요청입니다.","/");
                return;
            }

            key = SecurityUtils.Base64Decode(key, System.Text.Encoding.UTF8);
            //Dictionary<string, string> info = GetInvitationCodeAndUserID(key);

            Join j = new Join();
            int systemCode =  j.IsVerifyMember(key, id);

            if (systemCode == 0)
                ErrorMessage("인증키가 올바르지 않습니다.", "/");
            else if (systemCode == 1)
                ErrorMessage("이미 인증 처리된 코드입니다.", "/");
            else if (systemCode == 2)
                Response.Redirect("/Members/Welcome.aspx");

        }

        private Dictionary<string, string> GetInvitationCodeAndUserID(string security)
        {
            Dictionary<string, string> result =  new Dictionary<string, string>();
            string[] userinfo = security.Split('&');
            foreach (string str in userinfo)
            {
                string[] substr =  str.Split('=');
                result.Add(substr[0].Trim(), substr[1].Trim());
            }

            return result;
        }

        private void ErrorMessage(string message, string redirectUrl)
        {
            message = string.Format("<script language=javascript>alert('{0}'); location.href='{1}';</script>", message, redirectUrl);
            ClientScript.RegisterClientScriptBlock(this.GetType(), "join", message);
        }
    }
}